﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Sunny.Controls
{
	/// <summary>
	/// SearcherWindow.xaml 的交互逻辑
	/// </summary>
	public partial class SearcherWindow : UserControl
	{
		//protected readonly System.Collections.Generic.IEnumerable<SearchItem> SearchItems;

		protected readonly Searcher.AdvanceSearcher AdvanceSearcher;
		//protected Searcher.AdvanceSearcher AdvanceSearcher
		//{
		//	get
		//	{
		//		if (this._AdvanceSearcher == null)
		//			this._AdvanceSearcher = new Searcher.AdvanceSearcher(this.SearchItems);
		//		return this._AdvanceSearcher;
		//	}
		//}

		public Sunny.Controls.Searcher.WhereCommand WhereCommand
		{
			get
			{
				return this.AdvanceSearcher.WhereCommand;
			}
		}

		//public SearcherWindow(Searcher.AdvanceSearcher advanceSearcher) :this()
		//{
		//	this._AdvanceSearcher = advanceSearcher;
		//	this.Searcher.Content = this.AdvanceSearcher;
		//}

		public SearcherWindow(System.Collections.Generic.IEnumerable<SearchItem> searchItems):this()
		{
			this.Searcher.Content = this.AdvanceSearcher = new Controls.Searcher.AdvanceSearcher(searchItems);
		}

		private SearcherWindow()
		{
			InitializeComponent();
		}

		protected override void OnVisualParentChanged(DependencyObject oldParent)
		{
			((Window)this.Parent).Closed += SearcherWindow_Closed;
			base.OnVisualParentChanged(oldParent);
		}

		private void SearcherWindow_Closed(object sender, EventArgs e)
		{
			((Window)sender).Content = null;
		}

		private void AddSearchItem_Click(object sender, RoutedEventArgs e)
		{
			this.AdvanceSearcher.AddRow();
		}

		private void Submit_Click(object sender, RoutedEventArgs e)
		{
			Window window = (Window)this.Parent;

			window.DialogResult = true;
			window.Close();
		}
	}
}
